﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Web.Mvc;
using PaymentHelper.Methods.PaypalDirect.Models;
using PaymentHelper.Core.MVCUtils;

namespace PaymentHelper.Methods.PaypalDirect.Controllers
{
    public class PaypalDirectController : PaymentHelperController
    {
        protected override string SystemName
        {
            get { return "PaymentHelper.Methods.PaypalDirect"; }
        }

        [ChildActionOnly]
        public ActionResult Configure()
        {
            var settings = PaymentManager.Instance.LoadSettings(SystemName);

            var configModel = new ConfigModel();
            configModel.InitModel(settings);

            return View("PaymentHelper.Methods.PaypalDirect.Views.PaypalDirect.Configure", configModel);
        }

        [HttpPost]
        [ChildActionOnly]
        public ActionResult Configure(ConfigModel model)
        {
            if (!ModelState.IsValid)
                return View("PaymentHelper.Methods.PaypalDirect.Views.PaypalDirect.Configure", model);

            var listAtrr = model.ToListAttribute();
            bool isSuccess = PaymentManager.Instance.SaveSettings(SystemName,listAtrr);

            if (isSuccess)
                ViewBag.PaypalStandardMessage = "Changes are saved successfully !";
            else
                ViewBag.PaypalStandardMessage = "Changes are saved unsuccessfully !";

            return View("PaymentHelper.Methods.PaypalDirect.Views.PaypalDirect.Configure", model);
        }

        [ChildActionOnly]
        public ActionResult PaymentInfo()
        {
            var selectList = new List<SelectListItem>() {
                new SelectListItem(){ Selected = true, Text = "Visa", Value = "Visa"},
                new SelectListItem(){ Selected = false, Text = "MasterCard", Value = "MasterCard"},
                new SelectListItem(){ Selected = false, Text = "Discover", Value = "Discover"},
                new SelectListItem(){ Selected = false, Text = "Amex", Value = "Amex"},
                new SelectListItem(){ Selected = false, Text = "Maestro", Value = "Maestro"},
                new SelectListItem(){ Selected = false, Text = "Solo", Value = "Solo"},
                new SelectListItem(){ Selected = false, Text = "Switch", Value = "Switch"}
            };

            var model = new PaymentModel() { ListCard = selectList };
            var method = PaymentManager.Instance.LoadPaymentMethodBySystemName(SystemName);
            ViewBag.Resources = method.Descriptor.GetDefaultLanguage();

            return PartialView("PaymentHelper.Methods.PaypalDirect.Views.PaypalDirect.PaymentInfo",model);
        }

        public override IList<string> ValidatePaymentInfo(FormCollection frmData)
        {
            string cvv = frmData["CreditCardCVV2"].Trim();
            string expMonth = frmData["ExpMonth"].Trim();
            string expYear= frmData["ExpYear"].Trim();
            string ownerName = frmData["CardOwnerName"].Trim();
            string creditCardNum = frmData["CreditCardNumber"].Trim();
            string cardType = frmData["CreditCardType"].Trim();

            var method = PaymentManager.Instance.LoadPaymentMethodBySystemName(SystemName);
            var languagePackage = method.Descriptor.GetDefaultLanguage();

            List<string> listError = new List<string>();

            if (string.IsNullOrEmpty(cvv))
                listError.Add(languagePackage["PaypalDirect.InvalidCVV"]);

            if (string.IsNullOrEmpty(ownerName))
                listError.Add(languagePackage["PaypalDirect.InvalidOwnerName"]);

            if (string.IsNullOrEmpty(creditCardNum))
                listError.Add(languagePackage["PaypalDirect.InvalidCreditNumber"]);

            int month = 0;
            int year = 0;

            if (Int32.TryParse(expMonth, out month) == false)
            {
                listError.Add(languagePackage["PaypalDirect.InvalidExpMonth"]);
            }
            else
            {
                if (month < 1 || month > 12)
                    listError.Add(languagePackage["PaypalDirect.InvalidExpMonth"]);
            }

            if (Int32.TryParse(expYear, out year) == false)
            {
                listError.Add(languagePackage["PaypalDirect.InvalidExpYear"]);
            }
            else 
            {
                if (year < DateTime.Now.Year)
                    listError.Add(languagePackage["PaypalDirect.InvalidExpYear"]);
            }          
  
            return (listError.Count == 0) ? null : listError;
        }
    }
}
